use leptos::{context::Provider, prelude::*};
use std::collections::HashSet;
use thaw_utils::{BoxOneCallback, Model};

#[component]
pub fn CheckboxGroup(
    #[prop(optional, into)] value: Model<HashSet<String>>,
    #[prop(optional, into)] on_change: Option<BoxOneCallback<HashSet<String>>>,
    children: Children,
) -> impl IntoView {
    Effect::new(move |_|{
        if let Some(on_change) = on_change.as_ref() {
            on_change(value.get());
        }
    });
    view! {
        <Provider value=CheckboxGroupInjection(value)>
            {children()}
        </Provider>
    }
}

#[derive(Clone)]
pub(crate) struct CheckboxGroupInjection(pub Model<HashSet<String>>);

pub(crate) fn use_checkbox_group() -> CheckboxGroupInjection {
    expect_context()
}
